﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{
    internal class TipsJardinProvider
    {
        private ArbolandiaDBDataContext _db;

        internal TipsJardinProvider()
        {
            _db = new ArbolandiaDBDataContext();
        }

        internal List<Arb_TipsJardin> GetAllTipsJardin()
        {
            var query = _db.Arb_TipsJardin.Select(w => w);
            return query.Count() > 0 ? query.ToList() : new List<Arb_TipsJardin>();
        }
        internal List<TipsJardinCategory> GetAllCategorysTipsJardin()
        {
            var query = _db.Arb_TipsJardin.GroupBy(w => w.Category).Select(y => new TipsJardinCategory { Title = y.Key, Total = y.Count() });
            return query.Count() > 0 ? query.ToList() : new List<TipsJardinCategory>();
        }
        internal List<Arb_TipsJardin> GetAllTipsJardin(string category)
        {
            var query = _db.Arb_TipsJardin.Where(w => w.Category == category);
            return query.Count() > 0 ? query.ToList() : new List<Arb_TipsJardin>();
        }
        internal Int32 GetTotalTipsJardin()
        {
            return _db.Arb_TipsJardin.Count();
        }
        internal void SaveTipJardin(Arb_TipsJardin tip)
        {
            var query = _db.Arb_TipsJardin.Where(w => w.Id == tip.Id);
            if (query.Count() > 0)
            {
                query.First().Category = tip.Category;
                query.First().Content = tip.Content;
                query.First().Title = tip.Title;
            }
            else
            {
                _db.Arb_TipsJardin.InsertOnSubmit(tip);
            }
            _db.SubmitChanges();
        }
        internal void DeleteTipJardin(int id)
        {
            var query = _db.Arb_TipsJardin.Where(w => w.Id == id);
            if (query.Count() > 0)
            {
                _db.Arb_TipsJardin.DeleteOnSubmit(query.First());
                _db.SubmitChanges();
            }
        }
    }
}
